
if Talents == nil then
    Talents = {}
    Talents.__index = Talents
end

function Talents:new( o )
    o = o or {}
    setmetatable( o, Talents )
    return o
end

function Talents:Init()

end

function Talents:RefreshTalents(hero)
    local talentTable={}
    for j=0,GameConfig.abilitiesSlotCount-1 do
        local ability=hero:GetAbilityByIndex(j)
        local talents={}
        if ability~=nil then
            local kvTable=ability:GetAbilityKeyValues()
            if kvTable.AbilitySpecial  then
                for _,v in pairs(kvTable.AbilitySpecial) do
                    if v.LinkedSpecialBonus~=nil then
                        local talent={}
                        talent.ability=ability:GetAbilityName()
                        talent.name=v.LinkedSpecialBonus
                        local talentAbility=hero:FindAbilityByName(v.LinkedSpecialBonus)
                        if talentAbility==nil then
                            talentAbility=hero:AddAbility(v.LinkedSpecialBonus)
                        end
                        talent.value=talentAbility:GetLevelSpecialValueFor("value",0)
                        talent.learned=talentAbility:GetLevel()>=1
                        talent.canLearn=(talent.learned~=true) and (ability:GetLevel()==ability:GetMaxLevel())
                        table.insert(talents,talent)
                    end
                end
            end
        end
        if talents[1]==nil then
            talents[1]={}
        end
        if talents[2]==nil then
            talents[2]={}
        end
        if talents[1].learned==true or talents[1].learned==1 then
            talents[2].canLearn=false
        end
        if talents[2].learned==true or talents[2].learned==1 then
            talents[1].canLearn=false
        end

        table.insert(talentTable,talents)
    end
    --table.print(talentTable)
    CustomNetTables:SetTableValue(NetTableNames.talent_manager,tostring(hero:GetPlayerID()), talentTable)
end

function Talents:RemoveAbilityTalent(hero,ability)
    local talents=Talents:GetAbilityTalent(ability)
    if talents~=nil and #talents>0 then
        for _,value in pairs(talents) do
            local abilityTalent=hero:FindAbilityByName(value)
            if abilityTalent~=nil then
                hero:RemoveAbility(value)
            end
        end
    end
end

function Talents:OnAbilityChanged(hero)


end

function Talents:GetAbilityTalent(ability)
    local kvTable=ability:GetAbilityKeyValues()
    local linkedSpecialBonus={}
    if kvTable.AbilitySpecial  then
        for _,v in pairs(kvTable.AbilitySpecial) do
            if v.LinkedSpecialBonus~=nil then
                table.insert(linkedSpecialBonus,v.LinkedSpecialBonus)
            end
        end
    end
    return linkedSpecialBonus
end


function Talents:OnLearnTalent(data)
    --table.print(data)
    local hero=Hero.heroList[tonumber(data.playerId)]
    if hero~=nil then
        local talentTable= CustomNetTables:GetTableValue(NetTableNames.talent_manager, tostring(data.playerId))
        if talentTable~=nil then
            local talentInfo= talentTable[tostring(data.row)][tostring(data.col)]
            if talentInfo~=nil then
                local col2=data.col==1 and 2 or 1
                local talentInfo2=talentTable[tostring(data.row)][tostring(col2)]
                if talentInfo2~=nil and talentInfo2.learned==true then
                    return
                end
                --table.print(talentInfo)
               local talentAbility= hero:FindAbilityByName(talentInfo.name)
                if talentAbility~=nil  then
                    local ability= hero:FindAbilityByName(talentInfo.ability)
                    if ability~=nil and (ability:GetLevel()>=ability:GetMaxLevel()) then
                        talentAbility:SetLevel(1)
                        talentInfo.canLearn=false
                        talentInfo.learned=true
                        talentTable[tostring(data.row)][tostring(data.col)]=talentInfo
                        if talentInfo2~=nil then
                            talentInfo2.canLearn=false
                            talentInfo2.learned=false
                            talentTable[tostring(data.row)][tostring(col2)]=talentInfo2
                        end
                        CustomNetTables:SetTableValue(NetTableNames.talent_manager, tostring(data.playerId),talentTable)
                    end
                end
            end
        end
    end
end